package com.banggood.upms.converter;

import com.banggood.upms.bean.po.User;
import com.banggood.upms.bean.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XuChuangFeng
 * @date 2017/6/30
 */
@Component
public class UserConverter {

    public User singleInputConverter(UserVo vo) {
        return voToPo(vo);
    }

    public List<User> inputConverter(List<UserVo> voList) {
        List<User> poList = new ArrayList<>();
        for(UserVo vo : voList) {
            poList.add(voToPo(vo));
        }
        return poList;
    }

    public List<UserVo> outputConverter(List<User> poList) {
        List<UserVo> voList = new ArrayList<>();
        for(User po : poList) {
            voList.add(poToVo(po));
        }
        return voList;
    }

    public UserVo singleOutputConverter(User po) {
        return poToVo(po);
    }

    private UserVo poToVo(User po) {
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    private User voToPo(UserVo vo) {
        User po = new User();
        BeanUtils.copyProperties(vo, po);
        return po;
    }
}
